/*
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <string.h>
#include <cstdio>
#include <stdlib.h>
#include <stdio.h>
#include <climits>
#include <sys/types.h>
#include <gtest/gtest.h>
#include <dirent.h>
#include <unistd.h>
#include <securec.h>
#include "It_process_plimits.h"

#define PAGESIZE 4096
#define TESTSIZE 11534336

char *testStr1 = NULL;

static int limitWrite(const char *filepath, const char *buf)
{
    int fd = open(filepath, O_WRONLY);
    size_t ret = write(fd, buf, (strlen(buf) + 1));
    close(fd);
    return ret;
}

static int CloneOne(void *arg)
{
    std::string path = "/proc/plimits/test";
    std::string limitTestPath = "/proc/plimits/test/memory.limit";
    std::string procsTestPath = "/proc/plimits/test/plimits.procs";
    std::string failcntTestPath = "/proc/plimits/test/memory.failcnt";
    std::string usageTestPath = "/proc/plimits/test/memory.usage";
    std::string peakTestPath = "/proc/plimits/test/memory.peak";
    char buf[512];
    char writeBuf[8];

    testStr1 = (char *)malloc(TESTSIZE);
    memset(writeBuf, 0, sizeof(writeBuf));
    sprintf_s(writeBuf, sizeof(writeBuf), "%d", getpid());
    int ret = limitWrite(procsTestPath.c_str(), writeBuf);
    ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
    memset_s(testStr1, TESTSIZE, 1, TESTSIZE);
    sleep(5);
    memset(buf, 0, sizeof(buf));
    ret = ReadFile(usageTestPath.c_str(), buf);
    int pages = (TESTSIZE + PAGESIZE) / PAGESIZE;
    int actualPages = atoi(buf) / PAGESIZE;
    ICUNIT_ASSERT_WITHIN_EQUAL(actualPages, pages - 2, pages + 2, ret);
    return 0;
}
static UINT32 Testcase(void)
{
    mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
    int ret, pid, status;
    std::string path = "/proc/plimits/test";
    std::string limitTestPath = "/proc/plimits/test/memory.limit";
    std::string failcntTestPath = "/proc/plimits/test/memory.failcnt";
    std::string usageTestPath = "/proc/plimits/test/memory.usage";
    std::string peakTestPath = "/proc/plimits/test/memory.peak";
    std::string procsTestPath = "/proc/plimits/test/plimits.procs";
    char buf[512];
    const char *buffer = "10485760";
    char *stack = NULL;
    int twoM = 10 * 1024 * 1024;

    stack = (char *)mmap(NULL, twoM, PROT_READ | PROT_WRITE,
                         MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
    ICUNIT_ASSERT_NOT_EQUAL(stack, NULL, 1);
    ret = access(path.c_str(), 0);
    ICUNIT_ASSERT_EQUAL(ret, -1, ret);
    ret = mkdir(path.c_str(), S_IFDIR | mode);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    ret = limitWrite(limitTestPath.c_str(), buffer);
    ICUNIT_GOTO_NOT_EQUAL(ret, -1, ret, EXIT);
    pid = clone(CloneOne, stack, CLONE_PARENT, NULL);
    waitpid(pid, &status, 0);
    sleep(5);
    (void)memset(buf, 0, sizeof(buf));
    ret = ReadFile(failcntTestPath.c_str(), buf);
    ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
    (void)memset(buf, 0, sizeof(buf));
    ret = ReadFile(peakTestPath.c_str(), buf);
    ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
EXIT:
    free(testStr1);
    (void)RmdirControlFile(path);
    ret = rmdir(path.c_str());
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    return 0;
}

void ItProcessPlimitsMemory001(void)
{
    TEST_ADD_CASE("IT_PROCESS_PLIMITS_MEMORY_001", Testcase, TEST_PROCESS, TEST_PLIMITS, TEST_LEVEL0, TEST_FUNCTION);
}
